
package com.tony.common.web.upload;

import org.apache.log4j.Logger;

import java.io.File;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;
import org.apache.struts.actions.DownloadAction.FileStreamInfo;
import org.apache.struts.actions.DownloadAction.StreamInfo;

import util.StringUtil;



public class SubDownLoadAction extends DownloadAction {
	
	/** The Constant logger. */
	private static final Logger logger = Logger.getLogger(SubDownLoadAction.class);
	
	
	
	
	/** (non-Javadoc)
	 * @see org.apache.struts.actions.DownloadAction#getStreamInfo(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		String url = request.getParameter("url");
		String filename = request.getParameter("name");
		
//      if filename is passed by parameter ,then it's encoding is already iso-8859-1,
//		    so need not convert. but if it is get from DB, then need convert to iso-8859-1.
/*		try {
			filename = new String(filename.getBytes(),"iso-8859-1");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		response.setHeader("Content-disposition", 
                "attachment; filename="+filename);
		String contentType = "application/x-msdownload";
        File file = new File(url);

        return new FileStreamInfo(contentType, file);

	}
}
